package jermedit.gobjekte;

import java.awt.BasicStroke;
import java.awt.Graphics2D;
import java.awt.Polygon;
import java.awt.geom.Line2D;


public class RepresentedBy extends LineObject{
    private Polygon pfeil;
    private Line2D.Double pfeil1;
    private Line2D.Double pfeil2;
    private Math m;
    private double phi;      // Rotationswinkel 
    private double s = 10.0d;      // Seitenlaenge des Pfeils 
    private double xAlt, yAlt;          // x-,y-Werte vor der Rotation,relativ zur Pfeilspitze 
    private double xNeu, yNeu;          // x-,y-Werte nach der Rotation,absolut 
    private double xSpitze, ySpitze;    // x-,y-Werte der Pfeilspitze 
    
    public RepresentedBy(int x1, int y1, int x2, int y2){
        super(x1,y1,x2,y2);
        genArrow();
    }

    public void drawSubStroke(Graphics2D g2){
        g2.setStroke(
            new BasicStroke(1.0f,       // Width 
            BasicStroke.CAP_SQUARE,    // End cap 
            BasicStroke.JOIN_MITER,    // Join style 
            10.0f,                     // Miter limit 
            new float[] {5.0f,3.0f}, // Dash pattern //{length of Strokes, length of empty spaces}   
            0.0f)
        ); 
    }
    
    @Override
    public void drawSub(Graphics2D g2) {
        // Die Wahl eines Striches, den wir zeichnen, soll als Strich-Variable gespeichert werden
//        g2.setStroke(
//            new BasicStroke(1.0f,       // Width 
//            BasicStroke.CAP_SQUARE,    // End cap 
//            BasicStroke.JOIN_MITER,    // Join style 
//            10.0f,                     // Miter limit 
//            new float[] {5.0f,3.0f}, // Dash pattern //{length of Strokes, length of empty spaces}   
//            0.0f)
//        ); 
        g2.draw(new Line2D.Double(this.getVonX(),this.getVonY(),this.getNachX(),this.getNachY()));
        g2.draw(pfeil1);
        g2.draw(pfeil2);
        g2.setStroke(new BasicStroke()); // so setzen wir den Strich von g2 zurück
    }

    @Override
    public void setPointx(int point, int x, int y){
            super.setPointx(point, x, y);
            genArrow();
    }
    
    private void genArrow() {
        //Hier wird die Pfeilspitze generiert mithilfe der 2 Linienpunkte.
        phi = m.atan( ((double)this.getNachY()  -  (double)this.getVonY() ) / ((double)this.getNachX()  -  (double)this.getVonX() ) ); 
        if(this.getNachX() < this.getVonX() ){ 
            phi = phi + m.PI; 
        }
        xSpitze = (double)this.getNachX(); 
        ySpitze = (double)this.getNachY(); 
        // erster Punkt 
        xAlt = (double)this.getNachX() - s - xSpitze; 
        yAlt = (double)this.getNachY() + s/2.5 - ySpitze; 
        xNeu = xAlt * m.cos(phi) - yAlt * m.sin(phi) + xSpitze; 
        yNeu = xAlt * m.sin(phi) + yAlt * m.cos(phi) + ySpitze; 
        pfeil1 = new Line2D.Double((int)this.getNachX(), (int)this.getNachY(),(int)xNeu, (int)yNeu);
        // zweiter Punkt 
        yAlt = (double)this.getNachY() - s/2.5 - ySpitze; 
        xNeu = xAlt * m.cos(phi) - yAlt * m.sin(phi) + xSpitze; 
        yNeu = xAlt * m.sin(phi) + yAlt * m.cos(phi) + ySpitze; 
        pfeil2 = new Line2D.Double((int)this.getNachX(), (int)this.getNachY(),(int)xNeu, (int)yNeu);
    }
}
